<?php
require_once('inc/init.php');
require_once getCacheFilePath('forums.php');
require_once('inc/editor/ubb.php');
_header_("Content-type: application/xml");
$urlrewrite = intval($cache_settings['urlrewrite']);
$viewurl='';
$forumurl='';
switch($urlrewrite){
	case 0:
		$viewurl='view.php?tid={tid}';
		$forumurl='forum.php?fid={fid}';
		$userurl='user.php?uid={uid}';
	break;
	case 1:	//伪静态的方式1,程序实现
		$viewurl='?view-{tid}.html';
		$forumurl='?forum-{fid}.html';
		$userurl='?user-{uid}.html';
	break;
	case 2:	//伪静态的方式2,urlrewrite rule
		$viewurl="view-{tid}.html";
		$forumurl="forum-{fid}.html";
		$userurl="user-{uid}.html";
	break;
}

function _gmdate($dateint){
	global $cache_settings;
	$time_offset = $cache_settings['timeoffset'];
	return gmdate('r',$dateint+$time_offset*3600);
}
$view_fids=array();
foreach($cache_forums as $_forum){
	if(!empty($_forum['forumpass']) || !empty($_forum['forumusers']) || !empty($_forum['forumgroups'])){
		continue;
	}
	array_push($view_fids, $_forum['id']);
}

$fid=numFilter($_GET['fid']);
$uid=numFilter($_GET['uid']);
$basepath=getUrlPath(0);
$lastbuilddate = _gmdate(time());

$channellink='';
if(!empty($fid)){
	$isshow=true;
	$forum=$db->row_select_one("forums","id={$fid}");
	if(empty($forum)){
		$isshow=false;
	}
	if(!empty($forum['forumusers']) && !in_array($lg['username'],explode("\n",$forum['forumusers'])) && !isForumAdmin($fid)){
		$isshow=false;
	}
	if(!empty($forum['forumgroups']) && !in_array($lg['groupid'],explode(",",$forum['forumgroups'])) && !isForumAdmin($fid)){
		$isshow=false;
	}
	if(!empty($forum['forumpass']) && getCookies("forumpass_{$fid}")!=$forum['forumpass'] && !isForumAdmin($fid)){
		$isshow=false;
	}
	if($isshow){
		$condition=" topics.fid={$fid} and topics.isrecycle=0 and topics.verifystate=0 and topics.id=posts.tid and posts.istopic=1";
		$topicsrow = $db->row_select("topics,posts", $condition, 20, "*,topics.id as id", "topics.id desc");
	}else{
		$topicsrow = array();
	}
	$ofname=$cache_forums[$fid]['forumname'];
	$channellink=str_replace('{fid}',$fid, $forumurl);
}elseif(!empty($uid)){
	$isshow=true;
	if(getPopedom(2)==0){
		$isshow=false;
	}
	if($isshow){
		$condition=" topics.userid={$uid} and topics.isrecycle=0 and topics.verifystate=0 and topics.id=posts.tid and posts.istopic=1";
		$topicsrow = $db->row_select("topics,posts", $condition, 20, "*,topics.id as id", "topics.id desc");
	}else{
		$topicsrow = array();
	}
	$user=$db->row_select_one("users","id={$uid}");
	$ofname=$user['username'];
	$channellink=str_replace('{uid}',$uid, $userurl);
}

$topic_rows=array();
foreach($topicsrow as $key=>$topic){
	if(!in_array($topic['fid'],$view_fids)){
		continue;
	}
	$tid=$topic['id'];
	$topic['title']=htmlFilter($topic['title']);
	//$topic['content']='';
	$topic['content']=$ubb->ubb2html($topic['content']);
	$topic['posttime']=_gmdate($topic['posttime']);
	$topic['viewurl']=str_replace('{tid}',$topic['id'], $viewurl);
	array_push($topic_rows, $topic);
}

echo <<<EOT
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>{$ofname} - {$cache_settings['bbsname']}</title>
<link>{$basepath}/{$channellink}</link>
<description>Latest 20 topics of {$ofname}</description>
<copyright>Copyright(C) {$cache_settings['bbsname']}</copyright>
<generator>6KBBS,www.6kbbs.com</generator>
<lastBuildDate>{$lastbuilddate}</lastBuildDate>
<ttl>30</ttl>
<image>
	<url>{$basepath}/images/logo_88x31.gif</url>
	<title>{$ofname}</title>
	<link>{$basepath}</link>
</image>

EOT;
foreach($topic_rows as $topic){
$forumname=strip_tags($cache_forums[$topic['fid']]['forumname']);
echo <<<EOT
<item>
	<title>{$topic['title']}</title>
	<link>{$basepath}/{$topic['viewurl']}</link>
	<description><![CDATA[{$topic['content']}]]></description>
	<category>{$forumname}</category>
	<author>{$topic['username']}</author>
	<pubDate>{$topic['posttime']}</pubDate>
</item>

EOT;
}
echo <<<EOT
</channel>
</rss>
EOT;
?>